//
//  UFOLQCPlayCollectionViewCell.m
//  unrivalledForUniverse
//
//  Created by dllo on 1/13/16.
//  Copyright © 2016 dllo. All rights reserved.
//

#import "UFOLQCPlayCollectionViewCell.h"
#import "UFOLQCNewModel.h"
#import "UFOLQCNewTableViewCell.h"

@interface UFOLQCPlayCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableV;

@end

@implementation UFOLQCPlayCollectionViewCell

- (void)dealloc
{
    [_tableV release];
    [_dataArr release];
    _delegate = nil;
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [super dealloc];
}

- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr) {
        [_dataArr release];
    }
    _dataArr = [dataArr retain];
    [self createSubviews];
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}


- (void)createSubviews
{
    self.tableV = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    [self addSubview:self.tableV];
    [_tableV release];
    
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.delegate createRequesPlayData];
        [self.tableV.mj_header endRefreshing];
    }];
    
    self.tableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self.delegate createRequesPlayAddDate];
        [self.tableV.mj_footer endRefreshing];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataAction) name:@"playC" object:nil];
    
}

- (void)reloadDataAction
{
    [self.tableV reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellstr = @"cell";
    UFOLQCNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellstr];
    if (nil == cell) {
        cell = [[UFOLQCNewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr];
    }
    UFOLQCNewModel *newModel = [self.dataArr objectAtIndex:indexPath.row];
    
    NSURL *imageUrl = [NSURL URLWithString:newModel.image_url_small];
    [cell.image sd_setImageWithURL:imageUrl];
    if ([newModel.image_with_btn isEqualToString:@"True"]) {
        UIImageView *image = [UIImageView new];
        image.image = [UIImage imageNamed:@"iconfont-shipin-3"];
        [cell.image addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(cell.image).offset(-3);
            make.width.height.mas_equalTo(@20);
        }];
    }
    
    
    
    cell.titleL.text = newModel.title;
    cell.detailsL.text = [NSString stringWithFormat:@"%@\n ", newModel.summary];
    
    // 获取当前 时间 日期
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    NSInteger yearNow = [[dateString substringToIndex:4] integerValue];
    NSInteger dayNow = [[dateString substringWithRange:NSMakeRange(8, 2)] integerValue];
    NSInteger hourNow = [[dateString substringWithRange:NSMakeRange(11, 2)] integerValue];
    NSInteger minuteNow = [[dateString substringWithRange:NSMakeRange(14, 2)] integerValue];
    NSInteger yearL = [[newModel.publication_date substringToIndex:4] integerValue];
    NSInteger dayL = [[newModel.publication_date substringWithRange:NSMakeRange(8, 2)] integerValue];
    NSInteger hourL = [[newModel.publication_date substringWithRange:NSMakeRange(11, 2)] integerValue];
    NSInteger minuteL = [[newModel.publication_date substringWithRange:NSMakeRange(14, 2)] integerValue];
    if (0 != yearNow - yearL) {
        cell.pubDateL.text = [newModel.publication_date substringToIndex:10];
    } else if (0 == yearNow - yearL){
        if (2 < dayNow - dayL || 0 > dayNow - dayL) {
            cell.pubDateL.text = [[[newModel.publication_date substringWithRange:NSMakeRange(5, 6)] stringByReplacingOccurrencesOfString:@"-" withString:@"月"] stringByReplacingOccurrencesOfString:@" " withString:@"日"];
        } else if (0 < dayNow - dayL && 2>= dayNow - dayL){
            cell.pubDateL.text = [NSString stringWithFormat:@"%ld天前",dayNow - dayL];
        }else if (0 == dayNow - dayL){
            if (0 != hourNow - hourL) {
                cell.pubDateL.text = [NSString stringWithFormat:@"%ld小时前",hourNow - hourL];
            } else if (0 == hourNow - hourL){
                cell.pubDateL.text = [NSString stringWithFormat:@"%ld分钟前",minuteNow - minuteL];
            }
        }
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UFOLQCNewModel *newModel = [self.dataArr objectAtIndex:indexPath.row];
    if (30 >= newModel.article_url.length) {
        newModel.article_url = [NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@?APP_BROWSER_VERSION_CODE=1&android_version=1&imgmode=auto", newModel.article_url];
    }
    [self.delegate setupWebViewWithUrl:newModel.article_url];
}






@end
